//
//  CCTVSettingViewController.m
//  Keping
//
//  Created by 柯平 on 2017/7/1.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "CCTVSettingViewController.h"
#import "HJCCTVAddDeviceController.h"
#import "KCCTVRequest.h"


#import "CCTVSettingCell.h"

@interface CCTVSettingViewController ()

@end

@implementation CCTVSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
}
-(void)setUI
{
    self.navigationItem.title = @"CCTV Settings";
    self.groupView.refreshEnable = NO;
    self.groupView.loadmoreEnable = NO;
    self.groupView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.groupView.separatorInset = UIEdgeInsetsZero;
    [self.view addSubview:self.groupView];
    
    //添加尾部
    UIView*footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, K_FactorH(80))];
    self.groupView.tableFooterView=footerView;
    footerView.backgroundColor=[UIColor clearColor];
    CGFloat margin =K_FactorW(20);
    UIButton*addBtn=[[UIButton alloc]initWithFrame:CGRectMake(margin, K_FactorH(30), footerView.width-2*margin, footerView.height-K_FactorH(30))];
    [footerView addSubview:addBtn];
    addBtn.layer.cornerRadius=addBtn.height/2;
    addBtn.layer.masksToBounds=YES;
    addBtn.hidden = YES;
    [addBtn setBackgroundColor:[UIColor colorWithHexString:@"#6853fa"] forState:UIControlStateNormal];
    [addBtn setTitle:@"Add New Device" forState:UIControlStateNormal];
    addBtn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [addBtn addTarget:self action:@selector(clickAddDevice) forControlEvents:UIControlEventTouchUpInside];

}
-(void)clickAddDevice
{
    HJCCTVAddDeviceController*vc=[HJCCTVAddDeviceController new];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 40.0f;
//}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"Device Manager";
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cctvEntityViews.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CCTVSettingCell* cell = [tableView dequeueReusableCellWithIdentifier:[CCTVSettingCell className]];
    if (!cell) {
        cell = [[CCTVSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CCTVSettingCell className]];
    }
    __block KCctvEntityView *entity = [self.cctvEntityViews objectAtIndex:indexPath.row];
    cell.titleLabel.text = entity.name;
    cell.uiSwitch.on = [entity.status isEqualToString:@"Y"] ? YES : NO;
    WEAKSELF
    cell.switchBlock = ^(BOOL isOn) {
        [weakSelf updateCCTVStatuForEntityView:entity didSelectForOn:isOn];
    };
    return cell;
}

- (void)updateCCTVStatuForEntityView:(KCctvEntityView *)entity didSelectForOn:(BOOL)on {
    kCCTVSettingRequest *request = [[kCCTVSettingRequest alloc] initWithCctvPartyId:entity.cctvPartyId];
    request.params = @{@"status": on ? @"Y" : @"N"};
    WEAKSELF
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        NSDictionary *dict = request.responseObject;
        NSInteger status = [[dict objectForKey:@"statusCode"] integerValue];
        if (status == 200) {
            [weakSelf showSuccess:[dict objectForKey:@"msg"]];
            if (self.updateCCTVDatas) {
                self.updateCCTVDatas();
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf showError:request.error];
    }];
}

@end
